package com.cxm.report.config;

import com.cxm.report.model.IReport;
import com.cxm.report.model.criteria.ReportFilter;
import com.cxm.report.model.data.Report;
import com.cxm.report.model.event.MailEvent;
import com.cxm.report.service.BrandReportService;
import com.cxm.report.service.ChannelBrandReportService;
import com.cxm.report.service.ChannelReportService;
import com.cxm.report.service.EfficiencyReportService;
import com.cxm.report.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

/**
 * @author scl.BlueOcean
 * @version 1.0
 * @date 2018年07月05 17:26
 * @description 定时任务配置
 */
@Configuration
public class TaskConfig {
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private BrandReportService brandReportService;
    @Autowired
    private ChannelReportService channelReportService;
    @Autowired
    private EfficiencyReportService efficiencyReportService;
    @Autowired
    private ChannelBrandReportService channelBrandReportService;
    //每天01:00
    @Scheduled(cron = "0 0 1 * * ? ")
   // @Scheduled(cron = "0/5 * * * * ? ")
    public void refreshData(){
        //获取昨天的日期
        String date = DateUtil.getFutureDay(DateUtil.getDefaultFormat(),-1);
        try {
            brandReportService.refreshData(date);
        } catch (Exception e) {}
        try {
            channelReportService.refreshData(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            efficiencyReportService.refreshData(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            channelBrandReportService.refreshData(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //每天8点到23点，每5分钟执行一次刷新当天数据
    @Scheduled(cron = "0 0/5 8-23 * * ? ")
   // @Scheduled(cron = "0/5 * * * * ? ")
    public void refreshCurrentDay(){
        //获取昨天的日期
        String date = DateUtil.currentDay();
        try {
            brandReportService.refreshData(date);
        } catch (Exception e) {}
        try {
            channelReportService.refreshData(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            efficiencyReportService.refreshData(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            channelBrandReportService.refreshData(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //每天09:00
    @Scheduled(cron = "0 0 9 * * ? ")
    //@Scheduled(cron = "0/5 42 9 * * ? ")
    public void dayReport(){
        ReportFilter filter = new ReportFilter();
        try {
            List<Report> reports = brandReportService.dayReport(filter);
            Report report = reports.get(0);
            report.setType(IReport.Type.BRAND);
            applicationContext.publishEvent(new MailEvent(report));
        } catch (Exception e) {}
        try {
            List<Report> reports = channelReportService.dayReport(filter);
            Report report = reports.get(0);
            report.setType(IReport.Type.CHANNEL);
            applicationContext.publishEvent(new MailEvent(report));
        } catch (Exception e) {}
        try {
            List<Report> reports = channelBrandReportService.dayReport(filter);
            Report report = reports.get(0);
            report.setType(IReport.Type.CHANNEL_BRAND);
            applicationContext.publishEvent(new MailEvent(report));
        } catch (Exception e) {}
    }
    //每周一的09:00
    @Scheduled(cron = "0 0 9 ? * MON ")
    public void weekReport(){
        ReportFilter filter = new ReportFilter();
        String[] weekScope = DateUtil.getWeekScope(filter.getEnd());
        filter.setBegin(weekScope[0]);

        try {
            List<Report> reports = channelReportService.weekReport(filter);
            Report report = reports.get(0);
            report.setType(IReport.Type.CHANNEL);
            applicationContext.publishEvent(new MailEvent(report));
        } catch (Exception e) {}
    }
}
